#!/bin/sh
#
#

SPAMDDIR=%%SPAMDDIR%%
SPAMDUSER=%%SPAMDUSER%%
SPAMDGROUP=%%SPAMDGROUP%%
SPAMDUID=%%SPAMDUID%%
SPAMDGID=%%SPAMDGID%%

ask() {
  local question default answer

  question=$1
  default=$2
  if [ -z "${PACKAGE_BUILDING}" ]; then
    read -p "${question} [${default}]? " answer
  fi
  if [ "x${answer}" = "x" ]; then
    answer=${default}
  fi
  echo ${answer}
}

yesno() {
  local default question answer

  question=$1
  default=$2
  while :; do
    answer=$(ask "${question}" "${default}")
    case "${answer}" in
      [Yy][Ee][Ss]|[Yy])
        return 0
        ;;
      [Nn][Oo]|[Nn])
        return 1
        ;;
    esac
    echo "Please answer yes or no."
   done
}

check_db() {
    DB=/var/db/spamd
    if [ -f ${DB} ]; then
        OWN=`ls -l ${DB} | awk '{print $3}'`
        GRP=`ls -l ${DB} | awk '{print $4}'`
        if [ "x${OWN}" != "x${SPAMDUSER}" ]; then
            echo "change ${DB} owner to ${SPAMDUSER}"
            /usr/sbin/chown ${SPAMDUSER} ${DB}
        fi
        if [ "x${GRP}" != "x${SPAMDGROUP}" ]; then
            echo "change ${DB} group to ${SPAMDGROUP}"
            /usr/sbin/chown :${SPAMDGROUP} ${DB}
        fi
    fi
}

check_service() {
    local name number type comment

    name=$1
    number=$2
    type=$3
    comment=$4

    FILE="/etc/services"
    # check
    OK=no
    HAS_SERVICE=no
    COUNT=1
    for i in `grep $name $FILE `; do
    if [ $COUNT = 1 ] && [ X"$i" = X"$name" ]; then
        HAS_SERVICE=yes
    elif [ $COUNT = 2 ] && [ $HAS_SERVICE = yes ] && \
        [ X"$i" = X"$number/$type" ]; then
        OK=yes
        break
    fi
    COUNT=`expr ${COUNT} + 1`
    done
    # add an entry for SERVICE to /etc/services
    if [ $OK = no ]; then
    echo "This system has no entry for $name in ${FILE}"
    if yesno "Would you like to add it automatically?" y; then
        mv ${FILE} ${FILE}.bak
        (grep -v $name ${FILE}.bak ; \
        echo "$name 	$number/$type 	# $comment") \
        >> ${FILE}
        rm ${FILE}.bak
    else
        echo "Please add '$name $number/$type' into ${FILE}, and try again."
        return 1
    fi
    fi
    return 0
}


# this msg is for users installing not from source,
# - show msg about changed sync protocol
if [ "$2" = "PRE-INSTALL" ]; then
 echo "

    **** UPGRADE WARNING:  SYNC PROTOCOL CHANGED ****

    If you have spamd in distributed environment stop
    sync between old (4.1.x) and this new version
    until every node is updated

    read UPDATING in your PORTSDIR

    **** UPGRADE WARNING:  SYNC PROTOCOL CHANGED ****
"
    read -p "Hit ^c now to stop, any key to continue: " -t 5 a || true
    echo ""
    echo ""
fi

if [ "$2" = "PRE-INSTALL" ]; then
  check_dbown=0
  if /usr/sbin/pw group show "${SPAMDGROUP}" 2>&1 >/dev/null; then
    echo "You already have a \"${SPAMDGROUP}\" group, so I will use it."
    check_dbown=1
  else
    echo "You need a \"${SPAMDGROUP}\" group."
    if yesno "Would you like me to create it" "YES"; then
      /usr/sbin/pw groupadd "${SPAMDGROUP}" -g "${SPAMDGID}" -h - || \
        /usr/sbin/pw groupadd "${SPAMDGROUP}" -h - || exit
      echo "Done."
      check_dbown=1
    else
      echo "Please create the \"${SPAMDGROUP}\" group manually and try again."
      exit 1
    fi
  fi

  if /usr/sbin/pw user show "${SPAMDUSER}" 2>&1 >/dev/null; then
    echo "You already have a \"${SPAMDUSER}\" user, so I will use it."
    check_dbown=1
  else
    echo "You need a \"${SPAMDUSER}\" user."
    if yesno "Would you like me to create it" "YES"; then
      /usr/sbin/pw useradd "${SPAMDUSER}" -u "${SPAMDUID}" -g "${SPAMDGROUP}" -h - -d "${SPAMDDIR}" \
        -s /sbin/nologin -c "spamd pseudo-user" || \
        /usr/sbin/pw useradd "${SPAMDUSER}" -g "${SPAMDGROUP}" -h - -d "${SPAMDDIR}" \
        -s /sbin/nologin -c "spamd pseudo-user" || exit
      check_dbown=1
    else
      echo "Please create the \"${SPAMDUSER}\" user manually and try again."
      exit 1
    fi
  fi

  if [ ${check_dbown} -eq 1 ]; then
    check_db
  fi

  if ! check_service spamd 8025 tcp "spamd(8)"; then
    exit 1
  fi
  if ! check_service spamd-cfg 8026 tcp "spamd(8) configuration"; then
    exit 1
  fi
  if ! check_service spamd-sync 8025 udp "spamd(8) synchronisation"; then
    exit 1
  fi

fi
